// pages/index3/index3.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseURL:wx.baseURL,
    total:0,
    rows:[],
    brands:[],
    brandSelected:{},//已选品牌
    // 控制弹出层
    brandVisible:false,
    query:{
      key:'',//搜索关键字
      brandIds:[],
      pageNum:1,
      pageSize:10,
      sorted:0
    },
    key:''//搜索关键字
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //清空
  clearBrand(){
    this.setData({
      brandSelected:{}
    })
  },
  //切换品牌弹框
  changeBrandVisible(){
    let brandSelected = {}
    if(!this.data.brandVisible && this.data.query.brandIds){//打开品牌弹框
      //按照上一次选择结果初始化已选中品牌
      let brandIds = this.data.query.brandIds.split(',')
      for(let i = 0; i < brandIds.length; i++){
        brandSelected[brandIds[i]] = true
      }
      console.log(brandIds,brandSelected)
    }
    this.setData({
      brandVisible:!this.data.brandVisible,
      brandSelected
    })
  },

  //按品牌搜索
  confimBrand(){
    //直接刷新
    let brandIds = []
    for(let key in this.data.brandSelected){
      if( key && this.data.brandSelected[key]) brandIds.push(key)
    }
    this.data.query.brandIds = brandIds.join(',')
    this.refresh(()=>{
      this.setData({
        brandVisible:false
      })
    })
  },


  changeBrand(e){
    let brandSelected = this.data.brandSelected
    let id = e.currentTarget.dataset.id
    //取反，切换选择状态
    brandSelected[id] = !brandSelected[id]
    this.setData({
      brandSelected
    })
  },


  getList(next){//请求参数
    // let brandIds = []
    // for(let key in this.data.brandSelected){
    //   if( key && this.data.brandSelected[key]) brandIds.push(key)
    // }
    // this.data.query.brandIds = brandIds.join(',')
    wx.get('/products',this.data.query).then(res=>{
      res.rows.forEach((item,index)=>{
        item.img = wx.baseURL + item.productImgs.split(',')[0]
      })
      this.setData({
        total:res.total,
        rows:this.data.rows.concat(res.rows)//反复拉取连续
      })
      if(typeof next == 'function') next()
    })
  },

  // getList1(){
  //   wx.get('/products').then(res => {
  //     console.log(res)
  //     this.setData({
  //       ...res
  //     })
  //   })
  // },

  //修改顺序方式
  changeSorted(e){
    let sorted = e.currentTarget.dataset.sorted
    this.setData({
      'query.sorted':sorted
    })
    //刷新
    this.refresh()
  },

  //输入搜索关键字
  inputKey(e){
    this.data.query.key = e.detail.value
  },
  //重新加载
  refresh(next){
    this.data.query.pageNum = 1
    this.data.rows = []
    this.data.total = 0
    this.getList(next)
  },

  detail:function(e){
    wx.navigateTo({
      url:'/pages/index2/index2?productId=' + e.currentTarget.dataset.id,
    })
  },

  showBrands(){
    this.setData({
      brandVisible:!this.data.brandVisible
    })
  },
 
///获取品牌列表
  getBrands(){
    wx.get('/brands').then(res=>{
      this.setData({
        brands:res
      })
    })
  },

  onLoad: function (options) {
    console.log(options)
    this.setData({
      'query.key':options.key || '',
      'query.brandIds':options.brandIds || ''
    })
    this.getBrands()
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // this.data.query.pageNum = 1
    // this.data.rows = []
    // this.data.total = 0
    // this.getList(()=>{
    //   wx.stopPullDownRefresh()
    // })
    this.refresh(()=>{
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.query.pageNum++
    if(this.data.rows.length < this.data.total) this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})